package com.studentportalen;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

public class HomeActivity extends Activity {
	
	private int studentId;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	
    	Global.setDBHelper(new DBHelper(this));
    	
    	//Check if user is logged in, otherwise send to login screen
    	SharedPreferences sp = this.getSharedPreferences(Global.PREFS_NAME,MODE_PRIVATE);
    	Boolean loggedIn = sp.getBoolean("loggedIn", false);
    	studentId = sp.getInt("studentId", -1);
    	
    	if(!loggedIn){
    		//user not logged in, send to login screen
    		Intent intent = new Intent(HomeActivity.this, LoginActivity.class);
        	startActivity(intent);
        	//closes the current activity, and removes it from the activity stack
        	finish();
    	}
    	else if (studentId < 0){
    		Toast.makeText(getApplicationContext(), "Invalid student id, try logging in again.", Toast.LENGTH_LONG).show();
    		//invalid student id, send to login screen
    		Intent intent = new Intent(HomeActivity.this, LoginActivity.class);
        	startActivity(intent);
        	//closes the current activity, and removes it from the activity stack
        	finish();
    	}
    	else {
    	
	    	//Set database and current student
	    	//it needs more work
	        Global.setStudent(870312);
	    
	        setContentView(R.layout.activity_home);
    	}
    }
    
    /***
     * Opens the courses activity
     * @param view
     */
    public void openCourses(View view){
    	Intent intent = new Intent(HomeActivity.this, CoursesActivity.class);
    	startActivity(intent);
    }
    
    /***
     * Opens the exams activity
     * @param view
     */
    public void openExams(View view){
    	Intent intent = new Intent(HomeActivity.this, ExamsActivity.class);
    	intent.putExtra("studentId", studentId);
    	startActivity(intent);
    }
    
    /***
     * Opens the results activity
     * @param view
     */
    public void openResults(View view){
    	Intent intent = new Intent(HomeActivity.this, ResultsActivity.class);
    	startActivity(intent);
    }
    
    /***
     * Opens the results activity
     * @param view
     */
    public void openSchedule(View view){
    	Intent intent = new Intent(HomeActivity.this, ScheduleActivity.class);
    	startActivity(intent);
    }
    
    /***
     * Opens the results activity
     * @param view
     */
    public void openChooseCourse(View view){
    	Intent intent = new Intent(HomeActivity.this, ChooseCourseActivity.class);
    	startActivity(intent);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.layout.menu_home, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
            case R.id.menu_logout: 
	            	//Set user as logged in in shared preferences
	        		SharedPreferences sp = this.getSharedPreferences(Global.PREFS_NAME,MODE_PRIVATE);
	        		SharedPreferences.Editor editor = sp.edit();
	        		editor.putBoolean("loggedIn", false);
	        		editor.commit();
	        		
	        		//Start home activity
	    	    	Intent intent = new Intent(HomeActivity.this, LoginActivity.class);
	    	    	startActivity(intent);
	        		finish();
                    return true;
            case R.id.menu_help:
                    //show help
                    return true; 
            } 
            return false; //should never happen
    }

}